package com.shioirikukuri.service.impl;

import com.shioirikukuri.entity.Category;
import com.shioirikukuri.mapper.CategoryMapper;
import com.shioirikukuri.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CategoryServiceImpl implements ICategoryService {

	@Autowired
	private CategoryMapper categoryMapper; // 注入CategoryMapper用于数据库操作

	@Override
	public List<Category> getAllCategories() {
		return categoryMapper.selectAllCategories();
	}

	@Override
	public Optional<Category> getCategoryById(Long id) {
		return Optional.ofNullable(categoryMapper.selectCategoryById(id));
	}

	@Override
	public Category addCategory(Category category) {
		categoryMapper.insertCategory(category);
		return category;
	}

	@Override
	public Category updateCategory(Long id, Category category) {
		category.setId(id);
		categoryMapper.updateCategory(category);
		return category;
	}

	@Override
	public void deleteCategory(Long id) {
		categoryMapper.deleteCategoryById(id);
	}
}